package com.software.zsrt;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;


public class Test {
	/**
	 * @param args 序列化
	 */
	public static void main(String[] args) {
		File file=new File("e:\\books.xml");
		XmlSerializer xmlSerializer=new XmlSerializer();
		//序列化Book类型的集合
		List<Book> books=new ArrayList<Book>();
		books.add(new Book("完美世界","作者1",1200));
		books.add(new Book("寒门状元","作者2",1200));
		books.add(new Book("最强师兄","作者3",1200));
		xmlSerializer.serialize(file, books);
		//序列化和反序列化Student类型的集合
//		List<Student> students=new ArrayList<Student>();
//		System.out.println(students.getClass());
//		students.add(new Student("张三",20));
//		students.add(new Student("李四",22));
//		xmlSerializer.serialize(file, students);
//		readXml(file);
//		Student stu=xmlSerializer.deserialze(file, Student.class);
//		stu.hello();
//		List<Student> students=xmlSerializer.deserialze(file,ArrayList.class);
//		for(Student stu:students){
//			stu.hello();
//		}
	}
	/**
	 * 反序列化
	 */
	@SuppressWarnings("unchecked")
	private static void readXml(File file){
		SAXReader reader=new SAXReader();
		try{
			Document doc=reader.read(file);
			Element root=doc.getRootElement();
			for(Iterator<Element> iterator=root.elementIterator();iterator.hasNext();){
				System.out.println(iterator.next().getName());
			}
		}catch (DocumentException e){
			e.printStackTrace();
		}
	}


}
